<template>
  <div class="mask" v-if="cartListVisible&&calculations.total>0"
       @click="()=>{(calculations.total>0)&&toggleCartListVisible()}"></div>
  <div class="cart">
    <div class="product" v-if="cartListVisible&&calculations.total>0">
      <div class="product__header">
        <div class="product__header__all"
             @click="()=>setCartItemsChecked(shopId)">
          <i class="product__header__all__icon iconfont"
             v-html="calculations.allChecked ? '&#xe652;': '&#xe667;'"/>
          全选
        </div>
        <div class="product__header__clear">
          <span class="product__header__clear__btn" @click="()=>cleanCartProducts(shopId)">清空购物车</span>
        </div>
      </div>

      <div class="product__item" v-for="item in productList" :key="item._id">
        <i class="product__item__checked iconfont"
           v-html="item.check ? '&#xe652;': '&#xe667;'"
           @click="() => changeCartItemChecked(shopId, item._id)"
        />
        <img class="product__item__img" :src="item.imgUrl"/>
        <div class="product__item__detail">
          <h4 class="detail__title">{{ item.name }}</h4>
          <p class="detail__price">
            <span class="detail__price__yen">&yen;</span>{{ item.price }}
            <span class="detail__price__origin">&yen;{{ item.oldPrice }}</span>
          </p>
        </div>
        <div class="product__item__number">
        <span class="product__item__number__minus iconfont"
              @click="()=>changeCartItemInfo(shopId,item._id,item,-1)"
        >&#xe691;</span>
          {{ cartList?.[shopId]?.productList?.[item._id]?.count || 0 }}
          <span class="product__item__number__plus iconfont"
                @click="()=>changeCartItemInfo(shopId,item._id,item,1)"
          >&#xe668;</span>
        </div>
      </div>

    </div>

    <div class="cart__check">
      <div class="cart__check__icon">
        <img class="cart__check__icon__img" src="http://www.dell-lee.com/imgs/vue3/basket.png" alt=""
             @click="()=>{(calculations.total>0)&&toggleCartListVisible()}"/>
        <div class="cart__check__icon__tag">{{ calculations.total }}</div>
      </div>
      <div class="cart__check__info">
        总计: <span class="cart__check__info__price">&yen; {{ calculations.price }}</span>
      </div>
      <div class="cart__check__btn" v-show="calculations.total>0">
        <router-link :to="{path: `/orderConfirmation/${shopId}`}">
          去结算
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import {useStore} from 'vuex';
import {useRoute} from 'vue-router';
import {ref} from "vue";

import {useCommonCartEffect} from "../../effects/cartEffect";

// 获取购物车信息逻辑
const useCartEffect = (cartListVisible) => {
  const route = useRoute();
  const store = useStore();
  const shopId = route.params.id;
  const {changeCartItemInfo,cartList,calculations,productList} = useCommonCartEffect(shopId);

  const changeCartItemChecked = (shopId,productId) => {
    store.commit('changeCartItemChecked',{shopId,productId})
  };

  const cleanCartProducts = (shopId)=>{
    store.commit('cleanCartProducts',{shopId})
    cartListVisible.value = false;
  }

  const setCartItemsChecked = (shopId)=>{
    store.commit('setCartItemsChecked',{shopId})
  }

  return {productList,changeCartItemInfo,changeCartItemChecked,cleanCartProducts,setCartItemsChecked,calculations,cartList}
};

// 展示隐藏购物车逻辑
const useToggleCartEffect = ()=>{
  console.log('useToggleCartEffect');
  const cartListVisible = ref(false);

  const toggleCartListVisible = ()=>{
    cartListVisible.value = !cartListVisible.value;
  }

  return {cartListVisible, toggleCartListVisible}
}

export default {
  name: "Cart",
  setup(){
    const route = useRoute();
    const shopId = route.params.id;
    const {cartListVisible, toggleCartListVisible} = useToggleCartEffect();
    const {productList,changeCartItemInfo,changeCartItemChecked,cleanCartProducts,setCartItemsChecked,calculations,cartList} = useCartEffect(cartListVisible);

    return {productList, changeCartItemInfo, changeCartItemChecked,cleanCartProducts,setCartItemsChecked,calculations, shopId, cartListVisible, toggleCartListVisible, cartList};
  }
}
</script>

<style lang="scss" scoped>

@import '../../style/variables';
@import '../../style/mixins';

.mask {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 1;
}

.cart {
  position: absolute;
  z-index: 2;
  left: 0;
  right: 0;
  bottom: 0;

  background: $bgColor;

  &__check {
    display: flex;
    line-height: .49rem;
    height: .49rem;
    border-top: .01rem solid $content-bgColor;

    &__icon {
      width: .84rem;
      position: relative;

      &__img {
        display: block;
        margin: .12rem auto;
        width: .28rem;
        height: .26rem;
      }
      &__tag {
        position: absolute;
        height: .2rem;
        right: .2rem;
        top: .04rem;
        line-height: .2rem;
        background-color: $highlight-fontColor;
        border-radius: .1rem;
        font-size: .12rem;
        text-align: center;
        color: $bgColor;
        transform: scale(.5);

        //↓只要要适配 文字很长的情况
        transform-origin: left center;
        min-width: .2rem;
        padding: 0 .04rem;
        left: .46rem;
      }
    }
    &__info {
      flex: 1;
      color: $content-fontColor;
      font-size: .12rem;
      &__price {
        color: $highlight-fontColor;
        font-size: .18rem;
      }
    }
    &__btn{
      width: .98rem;
      line-height: .49rem;
      background-color: #4FB0F9;
      text-align: center;
      font-size: .14rem;

      a{
        text-decoration: none;
        color: $bgColor;
      }
    }
  }
}

.product {
  overflow-y: scroll;;
  flex: 1;
  background: $bgColor;

  &__header {
    border-bottom: .01rem solid $content-bgColor;
    line-height: .52rem;

    display: flex;

    font-size: .14rem;
    color: $content-fontColor;

    &__all {
      width: .64rem;
      margin-left: .18rem;
      display: flex;
      align-items: center;

      &__icon {
        color: $btn-bgColor;
        font-size: .2rem;
        margin-right: .084rem;
      }
    }

    &__clear {
      flex: 1;
      text-align: right;
      margin-right: .16rem;

      &__btn {
        display: inline-block;
      }
    }
  }

  &__item {
    position: relative;

    display: flex;
    padding: .12rem 0;
    margin: 0 .16rem;
    border-bottom: .01rem solid $content-bgColor;

    &__checked {
      color: $btn-bgColor;
      font-size: .2rem;
      line-height: .5rem;
      margin-right: .2rem;
    }

    &__img {
      width: .46rem;
      height: .46rem;
      margin-right: .16rem;
    }

    &__detail {
      overflow: hidden;

      .detail__title {
        margin: 0;
        line-height: .2rem;
        font-size: .14rem;
        color: $content-fontColor;

        @include ellipsis();
      }

      .detail__price {
        margin: .06rem 0 0 0;
        line-height: .2rem;
        font-size: .14rem;
        color: $highlight-fontColor;

        &__yen {
          font-size: .12rem;
        }

        &__origin {
          margin-left: .06rem;
          line-height: .2rem;
          font-size: .12rem;
          color: $light-fontColor;
          text-decoration: line-through;
        }
      }
    }

    &__number {
      position: absolute;
      right: 0;
      bottom: .26rem;

      &__minus, &__plus {
        //display: inline-block;
        //width: .2rem;
        //height: .2rem;
        //border-radius: 50%;
        //
        //font-size: .2rem;
        //text-align: center;
        //line-height: .16rem;
      }

      &__minus {
        //border: .01rem solid $medium-fontColor;
        color: $medium-fontColor;
        margin-right: .05rem;

        position:relative;
        top:.02rem;
      }

      &__plus {
        //background: $btn-bgColor;
        //color: $bgColor;
        margin-left: .05rem;

        color: $btn-bgColor;

        position:relative;
        top:.02rem;
      }
    }
  }
}
</style>
